<?php
namespace cms\modules\v1\models\feedback;

use common\models\Functions;
use common\models\Status;
use yii\base\Exception;
use Yii;
/**
 * This is the model class for table "goods".
 *
 * @property int $id
 * @property string $name
 */
class FeedbackType extends \common\models\feedback\FeedbackType
{

    public function getList() {
        try {
            $status = Yii::$app->request->get('status',0);
            $where = [];
            if (empty($result)) {
                if ($status) {
                    $where['status'] = $status;
                }
            }
            $result = static::getByWhere($where, ["id", "name", "status",'update_time','create_time'], 'id desc');
            if (!empty($result)) {
                foreach ($result as &$value) {
                    $value['time'] = $value['update_time'] ?: $value['create_time'];
                }
            }
            return Functions::formatRestAPI(0, '', $result);
        } catch (Exception $e) {
            return Functions::formatRestAPI(0, $e->getMessage());
        }
    }

    public function saveInfo() {
        try {
            $post = \Yii::$app->request->post();
            $id = (int) $post['id'];

            $insert = [];
            if (empty($post['name'])) {
                throw new Exception('类型不能为空');
            }
            $insert['name'] = $post['name'];
//            $insert['list_order'] = (int) $post['order'];

            if (empty($id)) {
                $insert['create_time'] = date("Y-m-d H:i:s");
                $res = static::insertData($insert);
                if ($res === false) {
                    throw new Exception("添加失败");
                }
            } else {
                $insert['update_time'] = date("Y-m-d H:i:s");
                $res = static::updateData($insert, ['id' => $id]);
                if ($res === false) {
                    throw new Exception("修改失败");
                }
            }

            return Functions::formatRestAPI(0, '操作成功');
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }

    public function getInfo($id = 0) {
        try {
            $info = static::getOneInfoById($id);

            if (empty($info)) {
                throw new Exception('未知分类信息');
            }

            $data['name'] = $info['name'];
            $data['status'] = $info['status'];
            $data['id'] = $info['id'];
            return Functions::formatRestAPI(0, '', $data);
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }

    public function setField() {
        try {
            $post = \Yii::$app->request->post();
            $id = (int) $post['id'];
            if (empty($id)) {
                throw new Exception("参数有误");
            }

            if (!empty($post['status'])) {
                $update['status'] = (int) $post['status'];
            }

            if (!empty($post['is_delete'])) {
                $update['is_delete'] = (int) $post['is_delete'];
            }

            $update['update_time'] = date("Y-m-d H:i:s");

            static::updateData($update, ['id' => $id]);

            return Functions::formatRestAPI(0, '操作成功');
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }
}
